﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace BOMS.WebUI.Web
{
    /// <summary>
    /// WarningDataHandler 的摘要说明
    /// </summary>
    public class WarningDataHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
          
            int TimeIntev = int.Parse(context.Request["TimeIntev"]);
            int WaringCount ;
            List<Model.S_Warning> li = new BLL.S_Warning().GetNewWarningData(TimeIntev, out WaringCount);
            string str = "{\"AllWaringCount\":" + WaringCount.ToString() + ",\"Data\":" + BuildStr(li) + "}";
            context.Response.ContentType = "text/plain"; 

           // context.Response.setCharacterEncoding("UTF-8");
           // response.setContentType("text/xml");
            context.Response.Write(str);

        }
        private string BuildStr(List<Model.S_Warning> li)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i = 0; i < li.Count; i++)
            {                
                sb.Append("{");
                sb.AppendFormat("\"HostName\":\"{0}\"", li[i].HostName);
                sb.AppendFormat(",\"BatteryGroup\":\"{0}\"", li[i].BatteryGroup);
                sb.AppendFormat(",\"BatteryId\":\"{0}\"", li[i].BatteryID);
                sb.AppendFormat(",\"WarningStatus\":\"{0}\"", Common.BuildWarningString(li[i].BatteryID == -1, li[i].WarningNO));
                sb.AppendFormat(",\"WarningTime\":\"{0}\"", li[i].WarningTime.ToString("yyyy-MM-dd HH:mm:ss"));
                sb.Append("}");
                if (i < li.Count - 1)
                {
                    sb.Append(",");
                }              
           } 
            sb.Append("]");
            return sb.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}